home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_058 / memclear / memclear.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  69 lines

  1. /* MemClear.c - (c) 1986 John Hodgson
  2.  
  3.        MemClear walks through the free memory lists zeroing along the way.
  4. Written mainly as an exercise, but may prove useful to some. This kind of
  5. system manipulation should never be performed in interrupt code; see RKM:Exec,
  6. Pg. 56 for more information.
  7.  
  8.     Memory management consists of a linked list of MemHeaders, each
  9. of which points to a linked list of MemChunks. The address of the first
  10. MemHeader is contained in ExecBase, yielding access to the whole mess.
  11.  
  12.     Each MemHeader oversees a hardware RAM partition, such as CHIP
  13. memory or a contiguous block of FAST RAM. It contains information regarding
  14. Attribute, (CHIP|FAST|PUBLIC) the block's address space, free space remaining,
  15. free list pointer, and link. The free bytes count is the sum total of all
  16. the Length fields in its free list; a linked list of MemChunks each containing
  17. a Length field and a link.
  18.  
  19.     Note that there are no actual pointers to the free space itself;
  20. Rather, the MemChunks are like Exec Messages in that there is a link node
  21. on top followed by 0..N bytes of free space. The MemChunk Length fields
  22. include the sizeof() the MemChunk structure itself, so a MemChunk cannot be
  23. shorter than 2 longwords. What AllocMem() does is searches each MemHeader that
  24. matches your attribute and looks for the FIRST MemChunk that fits. The node is
  25. unlinked from the chain, and its address returned. If the allocation was
  26. smaller than the node, the excess space is again partitioned up and linked
  27. into the MemChunk free list. The Length field of the MemHeader is decreased
  28. by the size of your allocation. Keep in mind that all allocations are rounded
  29. up to the nearest 8 bytes. Also, MemChunks are coalesced together whenever
  30. their address spaces are contiguous; it is imperative that the largest
  31. contiguous space possible be maintained for large-chunk applications such as
  32. bitmap allocation. */
  33.  
  34. extern struct ExecBase *SysBase;
  35.  
  36. main()
  37. {
  38.   struct MemHeader *memhdr;
  39.   struct MemChunk *memchunk;
  40.   long total=0,count=0;
  41.  
  42.   printf("MemClear (c) 1986 John Hodgson\n");
  43.   
  44.   Forbid(); /* task switching OFF */
  45.  
  46.   /* traverse linked list of MemHeaders */
  47.  
  48.   for (memhdr=(struct MemHeader *)SysBase->MemList.lh_Head;
  49.           memhdr->mh_Node.ln_Succ;
  50.               memhdr=(struct MemHeader *)memhdr->mh_Node.ln_Succ) {
  51.  
  52.     /* traverse linked list of MemChunks */
  53.  
  54.     for (memchunk=memhdr->mh_First;memchunk;memchunk=memchunk->mc_Next) {
  55.  
  56.       /* zero free space; but don't touch the node! */
  57.  
  58.       if (memchunk->mc_Bytes>sizeof(*memchunk))
  59.         setmem((char *)memchunk+sizeof(*memchunk),
  60.           memchunk->mc_Bytes-sizeof(*memchunk),0);
  61.  
  62.       total+=memchunk->mc_Bytes-sizeof(*memchunk); count++;
  63.     }  
  64.   }
  65.   Permit();
  66.  
  67.   printf("Cleared %d bytes in %d MemChunks.\n",total,count);
  68. }
  69.